package com.jxpanda.spring.module.wechat.component.http.service;

import com.jxpanda.infrastructure.core.tollkit.ReflectionKit;
import com.jxpanda.infrastructure.spring.toolkit.ReactiveHttpKit;
import com.jxpanda.spring.module.wechat.component.http.base.WechatHttpRequest;
import com.jxpanda.spring.module.wechat.config.properties.WechatConfigProperties;
import lombok.RequiredArgsConstructor;
import reactor.core.publisher.Mono;

import java.util.Map;


/**
 * @author Panda
 */
@RequiredArgsConstructor
public class WechatReactiveHttpService {

    protected final WechatConfigProperties wechatConfigProperties;

    protected <T> Mono<T> request(WechatHttpRequest<T> request) {
        return ReactiveHttpKit.builder()
                .url(request.getUrl())
                .method(request.getMethod())
                .queryHandler(queryParams -> queryHandler(queryParams, request.getQuery()))
                .body(request.getBody())
                .build()
                .execute(request.getReturnType());
    }

    private void queryHandler(Map<String, String> queryParams, Object queryObject) {
        if (queryObject != null) {
            Map<String, String> queryMap = ReflectionKit.objectToMapUsingJacksonProperty(queryObject);
            queryParams.putAll(queryMap);
        }
    }

}
